package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.presenter.contract.CustomerInfoContract;
import com.shd.edanpei.presenter.CustomerInfoPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/21.
 * 填写 客户个人信息
 */

public class CustomerInfoActivity extends BaseActivity<CustomerInfoPresenter> implements CustomerInfoContract.CustomerInfoIView {


    public static final int NEW_INFO = 100;
    public static final int UPDATA = 101;
    public static final int RESULT = 200;

    @BindView(R.id.headline)
    TextView headline;

    @BindView(R.id.et_name)
    TextView tvName;
    @BindView(R.id.tv_gender)
    TextView tvGender;
    @BindView(R.id.tv_birthday)
    TextView tvBirthday;
    @BindView(R.id.et_idNumber)
    TextView etIdNumber;
    @BindView(R.id.et_contact)
    EditText etContact;
    //    @BindView(R.id.et_bankNumber)
//    EditText etBankNumber;
    @BindView(R.id.radio_group_marital)
    RadioGroup radioGroupMarital;
    @BindView(R.id.ll_spouseName)
    View spouseName;
    @BindView(R.id.line_spouseName)
    View spouseNameLine;
    @BindView(R.id.et_spouseName)
    EditText etSpouseName;
    @BindView(R.id.ll_spouseContact)
    View spouseContact;
    @BindView(R.id.line_spouseContact)
    View spouseContactLine;
    @BindView(R.id.et_spouseContact)
    EditText etSpouseContact;
    @BindView(R.id.tv_salary)
    TextView tvSalary;
    @BindView(R.id.tv_industry)
    TextView tvIndustry;
    @BindView(R.id.tv_liveAddress)
    TextView tvLiveAddress;
    @BindView(R.id.et_detailAddress)
    EditText etDetailAddress;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    private int from;
    private Bundle extras;
    private String name;
    private String idNumber;
    private int mFrom;
    private String gender;
    private CustomerBean customerBean;


    @Override
    protected int setLayout() {
        return R.layout.activity_customer_info;
    }

    @Override
    protected CustomerInfoPresenter bindPresenter() {
        return new CustomerInfoPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {

        extras = getIntent().getExtras();
        mFrom = extras.getInt(Constant.FROM);
        if (mFrom == UPDATA) {
            headline.setText("修改客户个人信息");
            tvSubmit.setText("确认修改");
            mPresenter.getCustomerInfo(extras);

        } else if (mFrom == NEW_INFO) {
            headline.setText("客户个人信息");
            customerBean = extras.getParcelable(Constant.DATA);
            mPresenter.setCustomerBean(customerBean);
        }
    }


    @Override
    protected void initListener() {
//        etName.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
//                mPresenter.setName(s.toString());
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//
//            }
//        });
//        etIdNumber.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
//                etIdNumber.removeTextChangedListener(this);
//                etIdNumber.setText(s.toString().toUpperCase());
//                etIdNumber.setSelection(s.toString().length());
//                etIdNumber.addTextChangedListener(this);
//                mPresenter.setIdNumber(s.toString());
//                //TODO:
//                if (s.length() < 18) {
//                    if (s.toString().contains("x") || s.toString().contains("X")) {
//                        etIdNumber.getText().delete(s.length() - 1, s.length());
//                    }
//                }
//            }

//            @Override
//            public void afterTextChanged(Editable editable) {
//
//            }
//        });
        etContact.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                mPresenter.setContact(s.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
//        etBankNumber.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
//                mPresenter.setBankNumber(s.toString());
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//
//            }
//        });
        radioGroupMarital.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                boolean isShow = i == R.id.radio_button_yes;
                mPresenter.setMarital(isShow);
                spouseName.setVisibility(isShow ? View.VISIBLE : View.GONE);
                spouseNameLine.setVisibility(isShow ? View.VISIBLE : View.GONE);
                spouseContact.setVisibility(isShow ? View.VISIBLE : View.GONE);
                spouseContactLine.setVisibility(isShow ? View.VISIBLE : View.GONE);
            }
        });
        etSpouseName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                mPresenter.setSpouseName(s.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etSpouseContact.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                mPresenter.setSpouseContact(s.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etDetailAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                mPresenter.setDetailAddress(s.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @OnClick(R.id.iv_headline_back)
    void onBank() {
        finish();
    }
//
//    //选择性别
//    @OnClick(R.id.ll_gender)
//    void onGender() {
//        mPresenter.setGender(tvGender);
//    }
////
//    //选择出生日期
//    @OnClick(R.id.ll_birthday)
//    void onBirthday() {
//        mPresenter.setBirthday(tvBirthday);
//    }

    //当前收入
    @OnClick(R.id.ll_salary)
    void onSalary() {
        mPresenter.setSalary(tvSalary);
    }

    //行业
    @OnClick(R.id.ll_industry)
    void onIndustry() {
        mPresenter.setIndustry(tvIndustry);
    }

    //常住地址
    @OnClick(R.id.ll_liveAddress)
    void onLiveAddress() {
        mPresenter.setLiveAddress(tvLiveAddress);
    }

    @OnClick(R.id.tv_submit)
    void onSubmit() {
        if (mFrom == UPDATA) {
            mPresenter.updata(extras.getInt(Constant.INDEX));
        } else if (mFrom == NEW_INFO) {
            mPresenter.submit();
        }
    }

    @Override
    public void setText(TextView text, String str) {
        text.setText(str);
        text.setTextColor(ContextCompat.getColor(this, R.color.black));
    }

    @Override
    public TextView getSubmitView() {
        return tvSubmit;
    }

    @Override
    public void setData(CustomerBean bean) {
        tvName.setText(bean.getName());
        setText(tvGender, bean.getGender());
        if (mFrom != UPDATA) {
            setText(tvGender, bean.getGender());
        }
        setText(tvBirthday, bean.getBirthday());
        etIdNumber.setText(bean.getIdCardNo());

        if (!TextUtils.isEmpty(bean.getMobile())) etContact.setText(bean.getMobile());
        boolean isShow = bean.isMarrage();
        radioGroupMarital.check(isShow ? R.id.radio_button_yes : R.id.radio_button_no);
        spouseName.setVisibility(isShow ? View.VISIBLE : View.GONE);
        spouseNameLine.setVisibility(isShow ? View.VISIBLE : View.GONE);
        spouseContact.setVisibility(isShow ? View.VISIBLE : View.GONE);
        spouseContactLine.setVisibility(isShow ? View.VISIBLE : View.GONE);
        if (isShow) {
            setText(etSpouseName, bean.getSpouseName());
            setText(etSpouseContact, bean.getSpouseMobile());
        }
        if (!TextUtils.isEmpty(bean.getIncome())) setText(tvSalary, bean.getIncome());
        if (!TextUtils.isEmpty(bean.getIndustry())) setText(tvIndustry, bean.getIndustry());
        if (!TextUtils.isEmpty(bean.getAddress())) setText(tvLiveAddress, bean.getAddress());
        if (!TextUtils.isEmpty(bean.getDetailAddress())) etDetailAddress.setText(bean.getDetailAddress());
    }
}
